<?php

/**
 * RP System
 *
 * @version 1.0
 * @copyright 2004 Ron Planken
 */

class news {
	
	private $_system;
	
	function __construct($system) {
		$this->_system = $system;
	}

	
	function get($news_id = 0, $max = 0) {

		global $dbh;

	
		if ($news_id == 0) {
			$query ="SELECT news_id
						   ,subject
						   ,news.player_id
						   ,date_time_posted
						   ,text_body
						   ,name
					   FROM news
				 INNER JOIN player
						 ON news.player_id = player.player_id
				   ORDER BY date_time_posted DESC
							";
			if ($max > 0) {
				$query .= " LIMIT 0, $max";
			}
			
		} else {
			$query ="SELECT news_id
						   ,subject
						   ,news.player_id
						   ,date_time_posted
						   ,text_body
						   ,name
					   FROM news
				 INNER JOIN player
						 ON news.player_id = player.player_id
					  WHERE news_id = $news_id
							";
		}
		$newsItems = selectMultiQuery($query, $dbh);

		return $newsItems;
	}

	function save($news_id = 0, $subject = "", $player_id = 0, $date_time_posted = "", $text_body = "") {

		global $dbh;

		$subject = secure_string($subject);
		$player_id = secure_int($player_id);
		$text_body = secure_news($text_body);

		if ($news_id == 0) {

			$news_id = secure_int($news_id);

			$query ="INSERT INTO news
						   (subject
						   ,player_id
						   ,date_time_posted
						   ,text_body)
					VALUES ($subject
						   ,$player_id
						   ,$date_time_posted
						   ,$text_body)
							";
			$status = InsertQuery($query, $dbh);
		} else {
			$query ="UPDATE news
						SET subject = $subject
						   ,player_id = $player_id
						   ,date_time_posted = $date_time_posted
						   ,text_body = $text_body
					  WHERE news_id = $news_id
							";
			$status = UpdateQuery($query, $dbh);
		}

		return $status;

	}
	

	function remove($news_id = 0) {

		global $dbh;
		
		$news_id = secure_int($news_id);
		
		$query ="DELETE FROM news
				  	   WHERE news_id = $news_id;
							";
		$status = UpdateQuery($query, $dbh);


		
		return $status;
	}
	
}
?>